﻿// <copyright file="MonitorController.cs" company="Clever Software">
// Copyright (c) 2013, 2014 All Right Reserved, http://www.cleversoftware.by
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Clever Software</author>
// <email>info@cleversoftware.by</email>
// <date>2014-02-04</date> 
// <summary>CleverCMS source code</summary>

namespace CleverCMS.Web.Cms.Admin.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Mvc;

    using CleverCMS.Cms.Services;

    [Authorize(Roles = "Admin")]
    public class MonitorController : Controller
    {
        private readonly MonitorService m_monitorService;

        public MonitorController(MonitorService monitorService)
        {
            m_monitorService = monitorService;
        }

        public ActionResult List()
        {
            return View();
        }

        [HttpPost]
        public JsonResult ListLogs(int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null) 
        {
            try
            {
                int count = 0;
                var allLogs = m_monitorService.LoadAll(jtStartIndex, jtPageSize, jtSorting, out count).ToList();
                return Json(new
                {
                    Result = "OK",
                    Records = allLogs.Select(x => new
                    {
                        Id = x.ID,
                        Logger = x.Logger, 
                        Level = x.Level,
                        Message = x.Message,
                        Exception = x.Exception, 
                        Date = x.Date.ToString("dd-MM-yyyy hh:mm:ss") 
                    }),
                    TotalRecordCount = count
                });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }
    }
}